package com.xjtu.conan.controller;


import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.response.AjaxResponse;
import com.xjtu.conan.service.ArticleImagesInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 话题图片表 前端控制器
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/conan/article-images-info")
@Api(value = "话题对应图片管理模块", description = "这是话题对应图片信息CRUD的模块")
public class ArticleImagesInfoController {
    @Autowired
    private ArticleImagesInfoService articleImagesInfoService;

    @ApiOperation(value = "获取全部话题图片", notes = "获取所有话题图片的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAllArticleImages", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(articleImagesInfoService.getAllArticleImageInfo(current, size));
    }

    @ApiOperation(value = "根据id查找话题图片", notes = "获取根据id查找到的话题图片")
    @ApiImplicitParam(name = "aiid", value = "话题图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getArticleImageById", method = RequestMethod.GET)
    public AjaxResponse getArticleImageById(@RequestParam(name = "aiid") Long aiid) {
        return AjaxResponse.success(articleImagesInfoService.getArticleImageByid(aiid));
    }

    @ApiOperation(value = "根据话题id查找对应图片", notes = "获取根据话题id查找到的话题图片")
    @ApiImplicitParam(name = "aid", value = "话题id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getArticleImagesByaid", method = RequestMethod.GET)
    public AjaxResponse getArticleImageByaid(@RequestParam(name = "aid") Long aid) {
        return AjaxResponse.success(articleImagesInfoService.getArticleImagesByaid(aid));
    }

    @ApiOperation(value = "添加话题图片信息", notes = "获取话题图片信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "话题图片文件", required = true, paramType = "form", dataType = "file"),
            @ApiImplicitParam(name = "aid", value = "所属话题id", required = true, paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "/addArticleImages", method = RequestMethod.POST)
    public AjaxResponse addArticleImages(@RequestParam(value = "files") MultipartFile[] files,
                                      @RequestParam(value = "aid") Long aid,
                                      HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        return AjaxResponse.success("数据库受影响行数->" + articleImagesInfoService.addArticleImages(files, aid, loginUser));
    }

    @ApiOperation(value = "删除话题图片信息", notes = "从数据库中删除某个话题图片")
    @ApiImplicitParam(name = "aiid", value = "删除话题图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteArticleImage", method = RequestMethod.DELETE)
    public AjaxResponse deleteArticleImage(@RequestParam(value = "aiid") Long aiid) {
        return AjaxResponse.success("数据库受影响行数->" + articleImagesInfoService.deleteArticleImages(aiid));
    }

    @ApiOperation(value = "话题图片总数量", notes = "获取数据库中话题图片总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(articleImagesInfoService.getTotal());
    }
}

